package com.ray.service;

import com.ray.bean.*;
import com.ray.mapper.DingTalkMapper;
import com.ray.mapper.MaterialTypeCheckTypeMapper;
import com.sun.javafx.iio.common.ImageLoaderImpl;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MaterialTypeCheckTypeService {

    @Autowired
    MaterialTypeCheckTypeMapper materialTypeCheckTypeMapper;
    @Autowired
    DingTalkMapper dingTalkMapper;

    /**
     * 获取公司检验类型
     * @param map
     * @return
     */
    public List<CheckType> getCheckType(Map map) {
        return materialTypeCheckTypeMapper.getCheckType(map);
    }

    /**
     * 设置检查类型
     * @param map
     * @return
     */
    public String setCheckType(Map map) {
//        int deleteRow = materialTypeCheckTypeMapper.deleteCheckType(map);
        String jsonStr=map.get("checkArray").toString();
        JSONArray jsonArray=JSONArray.fromObject(jsonStr);
        Assert.isTrue(jsonArray.size()>0,"操作失败");
        List<String> checkArray=jsonArray.subList(0, jsonArray.size());
        map.put("checkArray",checkArray);
        int row = materialTypeCheckTypeMapper.addCheckType(map);
        if(row<=0){
            return "操作失败";
        }
        return "操作成功";
    }

    /**
     * 获取物料类型下的
     * @param map
     * @return
     */
    public List<MaterialTypeCheckType> getMaterialCheckType(Map map) {
        return materialTypeCheckTypeMapper.getMaterialCheckType(map);
    }

    /**
     * 设置物料检验类型
     * @param map
     * @return
     */
    public String setMaterialCheckType(Map map) {
        int deleteRow = materialTypeCheckTypeMapper.deleteMaterialCheckType(map);
        String jsonStr=map.get("checkArray").toString();
        JSONArray jsonArray=JSONArray.fromObject(jsonStr);
        List<MaterialTypeCheckType> list= (List<MaterialTypeCheckType>)JSONArray.toCollection(jsonArray, MaterialTypeCheckType.class);
        if(jsonArray.size()>0){
            List<String> checkArray=jsonArray.subList(0, jsonArray.size());
            map.put("checkArray",list);
            int row = materialTypeCheckTypeMapper.addMaterialCheckType(map);
            if(row<=0){
                return "操作失败";
            }
        }
        return "操作成功";
    }


    /**
     *获取需要qc的物料
     * @param map
     * @return
     */
    public DatatablesViewPageBean<CompanyMaterialTableBean> getNeedQcMaterial(Map map){
        int count = materialTypeCheckTypeMapper.getNeedQcMaterialCount(map);
        List<CompanyMaterialTableBean> companyMaterialTableBeans = materialTypeCheckTypeMapper.getNeedQcMaterial(map);
        DatatablesViewPageBean<CompanyMaterialTableBean> datatablesViewPageBean = new DatatablesViewPageBean<CompanyMaterialTableBean>();
        datatablesViewPageBean.setiTotalDisplayRecords(count);
        datatablesViewPageBean.setiTotalRecords(count);
        datatablesViewPageBean.setReturnData(companyMaterialTableBeans);
        return datatablesViewPageBean;
    }

    /**
     * 设置qc结果
     * @param map
     * @return
     */
    public String  setQcResult(Map map) {
        String checkReportNo = dingTalkMapper.getApprovalNo("QC");
        int isNeedApproval = Integer.parseInt(map.get("isNeedApproval").toString());
        map.put( "checkReportNo",checkReportNo);
        Integer status = 0;
        //如果不需要审批
        if(isNeedApproval == 2){
            status = 1;
            Integer conclusion = Integer.parseInt(map.get("conclusion").toString());
            Double qualityCount = Double.parseDouble(map.get("qualityCount").toString());
            Double noQualityCount = Double.parseDouble(map.get("noQualityCount").toString());
            Double count = this.getQcCount(conclusion,qualityCount,noQualityCount);
            if(count>0){
                //设置采购单已qc数
                this.setPurchaseQcCount(map.get("purhaseId").toString(),count);
            }
        }
        map.put("status",status) ;
        int mainRow = materialTypeCheckTypeMapper.insertQcResultMain(map);
        if(mainRow<=0){
            return "操作失败";
        }
        String jsonStr=map.get("checkArray").toString();
        JSONArray jsonArray=JSONArray.fromObject(jsonStr);
        List<QCResultBean> list= (List<QCResultBean>)JSONArray.toCollection(jsonArray, QCResultBean.class);
        map.put("checkArray",list);
        int row = materialTypeCheckTypeMapper.insertQcResultDetail(map);
        if(row<=0){
            return "操作失败";
        }
        return "操作成功";
    }

    /**
     * 设置采购单已qc数
     * @param purhaseId
     * @param count
     */
    private void setPurchaseQcCount(String purhaseId, Double count) {
        //查询采购单
        PurchaseOrderBean purchaseOrderBean = materialTypeCheckTypeMapper.getPurchaseOrder(purhaseId);
        //校验已qc数是否大于已采购数
        Assert.isTrue(purchaseOrderBean.getCheckCount() + count <=purchaseOrderBean.getPurchaseCount(),"最多可检验"+(purchaseOrderBean.getPurchaseCount()-purchaseOrderBean.getCheckCount()));
        if(purchaseOrderBean.getCheckCount() + count == purchaseOrderBean.getPurchaseCount()){
            purchaseOrderBean.setIsFinish(1);
        }
        purchaseOrderBean.setCheckCount(purchaseOrderBean.getCheckCount()+count);
        //修改已qc数
        materialTypeCheckTypeMapper.updatePurchase(purchaseOrderBean);
    }

    /**
     * 获取qc有效数量
     * @param conclusion
     * @param qualityCount
     * @param noQualityCount
     * @return
     */
    private Double getQcCount(Integer conclusion, Double qualityCount, Double noQualityCount) {
        if (null == conclusion){
            return 0.0;
        }
        //不接受
        Double returnCount = 0.0;
        //整批接受
        if(conclusion == 1 || conclusion == 3){
            returnCount =  qualityCount + noQualityCount;
        }
        //部分接受
        if(conclusion == 2 || conclusion == 4){
            returnCount = qualityCount;
        }
        return returnCount;
    }

    /**
     * 获取qc结果
     * @param map
     * @return
     */
    public DatatablesViewPageBean<QCResultMain> getQcResult(Map map) {
        int count = materialTypeCheckTypeMapper.getQcResultCount(map);
        List<QCResultMain> companyMaterialTableBeans = materialTypeCheckTypeMapper.getQcResult(map);
        DatatablesViewPageBean<QCResultMain> datatablesViewPageBean = new DatatablesViewPageBean<QCResultMain>();
        datatablesViewPageBean.setiTotalDisplayRecords(count);
        datatablesViewPageBean.setiTotalRecords(count);
        datatablesViewPageBean.setReturnData(companyMaterialTableBeans);
        return datatablesViewPageBean;
    }

    /**
     * 审核qc结果
     * @param map
     * @return
     */
    public String approvalQcResult(Map map) {
        Integer status = Integer.parseInt(map.get("status")==null?"0":map.get("status").toString());
        Assert.isTrue(status != 0,"未获取到状态");
        QCResultMain qcResultMain = materialTypeCheckTypeMapper.getQcResultById(map);
        Assert.isTrue(qcResultMain.getStatus() == 0,"已审核，不可再次审核");
        Double count = this.getQcCount(qcResultMain.getConclusion(),qcResultMain.getQualityCount(),qcResultMain.getNoQualityCount());
        if(count>0){
            //设置采购单已qc数
            this.setPurchaseQcCount(qcResultMain.getPurhaseId(),count);
        }
        int updateRow = materialTypeCheckTypeMapper.approvalQcResult(map);
        Assert.isTrue(updateRow>0,"操作失败");
        return "操作成功";
    }

    /**
     * 通过检验编号获取检验详情
     * @param map
     * @return
     */
    public List<QCResultBean> getDetailByCheckReportNo(Map map) {
        return materialTypeCheckTypeMapper.getDetailByCheckReportNo(map);
    }

    /**
     * 修改检验类型
     * @param map
     * @return
     */
    public String updateCheckType(Map map) {
        int row = materialTypeCheckTypeMapper.updateCheckType(map);
        Assert.isTrue(row>0,"操作失败");
        return "操作成功";
    }
}
